package com.jkc.xh.activities;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.ConfigResponse;
import com.jkc.xh.network.models.LoginResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.utils.SecurePreferences;
import com.jkc.xh.utils.Tool;
import com.jkc.xh.views.widget.ToastUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity {
    @ViewById(R.id.inputLoginName)
    EditText inputLoginName;
    @ViewById(R.id.inputLoginPwd)
    EditText inputLoginPwd;
    @ViewById
    TextView tvVersionName;
    @ViewById
    View registerLayout;

    @AfterViews
    void checkIsLogin() {
        tvVersionName.setText(getString(R.string.version_name, Tool.getAppVersion(this, false)));
        if (!TextUtils.isEmpty(UserManager.getLastLoginName())) {
            inputLoginName.setText(UserManager.getLastLoginName());
        }


        if (UserManager.isLogin()) {
            finish();
            startActivity(new Intent(LoginActivity.this, MainActivity_.class));
        } else {
            addJob(UserManager.getCpmfConfig(new RestCallBack<ConfigResponse>() {
                @Override
                public void success(ConfigResponse configResponse) {
                    if (configResponse.isOpenResister()) {
                        registerLayout.setVisibility(View.VISIBLE);
                    } else {
                        registerLayout.setVisibility(View.GONE);
                    }
                    SecurePreferences.getInstance().edit().putString("preference_page_url", configResponse.page_url).commit();
                    SecurePreferences.getInstance().edit().putString("preference_buy_tip", configResponse.buy_tip).commit();
                    SecurePreferences.getInstance().edit().putBoolean("preference_notice_popup_show", false).commit();
                }

                @Override
                public void failure(RestError error) {

                }
            }));
        }

    }

    @Click(R.id.btnLogin)
    public void btnLoginClicked() {
        String loginName = inputLoginName.getText().toString();
        String loginPwd = inputLoginPwd.getText().toString();
        if (TextUtils.isEmpty(loginName) || TextUtils.isEmpty(loginPwd)) {
            return;
        }
        showProgress("正在登陆");
        addJob(UserManager.login(loginName, loginPwd, new RestCallBack<LoginResponse>() {


            @Override
            public void success(LoginResponse loginResponse) {
                closeProgress();
                startActivity(new Intent(LoginActivity.this, MainActivity_.class));
                finish();
            }

            @Override
            public void failure(RestError error) {
                closeProgress();
            }
        }));
    }

    private boolean mIsExit = false;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mIsExit = false;
        }

    };

    @Override
    public void onBackPressed() {

        int count = getSupportFragmentManager().getBackStackEntryCount();
        if (count == 0) {
            if (!mIsExit) {
                mIsExit = true;
                ToastUtil.showToastShort(getString(R.string.exit_tip));
                mHandler.sendEmptyMessageDelayed(0, 2000);
            } else {
                finish();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Click(R.id.registerNow)
    public void btnRegisterClicked() {
        startActivity(new Intent(this, RegisterActivity_.class));
    }

//    @Click(R.id.forgetPwd)
//    public void forgetPwd() {
//        ForgetPwdFragment_.builder().build().startFragment(getBaseContext());
//    }

}
